/* eslint-disable no-unused-vars */
import React, { Component } from 'react';
import { View, DeviceEventEmitter } from 'react-native';
// import { Navigation } from 'react-native-navigation';
import { Navigation } from './modules/views/global/navigation/index';

import { Provider } from 'react-redux';
import initSubscriber from 'redux-subscriber';
import { registerScreens } from './screens';
// import { iconsMap, iconsLoaded } from './utils/AppIcons';
import configureStore from './store/configureStore';
// import SplashScreen from 'react-native-splash-screen';

// import EasemobChat from './modules/views/global/EasemobChat';
import DeviceStorage from './modules/views/global/DeviceStorage';
import {global, session} from './modules/actions';

const store = configureStore();
const subscribe = initSubscriber(store);

registerScreens(store, Provider);

const navigatorStyle = {
	navBarTextColor: 'white',
	navBarButtonColor: 'white',
	navBarBackgroundColor: '#EA5404',
	statusBarColor: '#EA5404',
	statusBarTextColorScheme: 'light',
	screenBackgroundColor: 'white',
	drawUnderTabBar: true,
	navBarTitleTextCentered: true,
	topBarElevationShadowEnabled:false
};

class App extends Component {
	constructor(props) {
		super(props);
		// 启动(store变更时调用)
	    // store.subscribe(this._onStoreUpdate.bind(this));
		this.unsession = subscribe('session', state => {

		    let { session } = state;

			if(session && session.user ){
				console.log('App=====constructor===session',session);
				if (session.user.group) {
					session.reload && this.startApp('main', session.user,session.stoken);
				}else{
					this.startApp('login');
				}
				
			}else{
				this.startApp('login');
			}
		});

		this.unwelcome = subscribe('global.welcomedone', state => {
		    let { global } = state;
			if(!global.welcomedone){
				this.startApp('welcome');
			}
		});

		store.dispatch(global.global_init());
		// 欢迎页面结束后，调用下面的语句，目前还没做
		store.dispatch(global.global_welcome_done());

		// After having done stuff (such as async tasks) hide the splash screen
		// SplashScreen.hide();

	}

	// _onStoreUpdate(){
	// 	let { global, session } = store.getState();
	//
	// 	if (!global.initialized){// app初始化
	//         // this.startApp('not-init');
	//     }else{
	//         if (!global.welcomedone) {
	//             this.startApp('welcome');
	//         } else {
	// 			if(session && session.user){//登录过的
	// 				session.reload && this.startApp('main');
	// 			}else{//没登录过或者失效
	// 				this.startApp('login');
	// 			}
	//         }
	//     }
	// }

	startApp(root, user,stoken) {
		console.log('App=====startApp===user',user);
		switch (root) {
	      case 'welcome':

	        break;
	       case 'login': //登录界面
	            Navigation.startSingleScreenApp({
	              screen: {
	                  screen: 'housekeeperapp.loginindex',
	                  animated: true,
	                  navigatorStyle: {
	                      navBarHidden:true,
						  statusBarBlur: true,
						  statusBarTextColorScheme: 'light'
	                  }
	              },
	              passProps: {
	              },
	              animationType: 'fade'
	            });
	            break;
	      case 'main':
		  		Navigation.startMainActivityApp({
					skill_group:user.group,
					user_id:user.id,
					productKey:user.product.key,
					user_role: user.role,
					ticket: user.ticket,
					easemob_kefu_name: user.easemob_kefu_name,
					easemob_kefu_pwd: user.easemob_kefu_pwd,
					village:user.village,
//=======
					// ticket:stoken,
//>>>>>>> master-temp
					tabs:[
						{
							label: '订单',
							screen: 'housekeeperapp.order',
							title: '订单',
							navigatorStyle:{
								...navigatorStyle,
								navBarTitleTextCentered: false,
								navBarHidden: true
							},
							// navigatorButtons:{
							// 	rightButtons:[
							// 		{
							// 			title: '新增任务',
							// 			id: 'add',
							// 		}
							// 	]
							// }
						},
						{
							label: '账户',
							screen: 'housekeeperapp.account',
							title: '我的账户',
							navigatorStyle:{
								...navigatorStyle,
								navBarHidden: true
							}
						}
					],
					appStyle: {
					   tabBarBackgroundColor: '#ffffff',
					   tabBarButtonColor: '#B5B5B5',
					   tabBarSelectedButtonColor: '#EA5404',
					   tabFontFamily: 'BioRhyme-Bold',
					   forceTitlesDisplay: true
					}
				});

				DeviceEventEmitter.addListener("logout", ()=>{
					store.dispatch(session.clear_session());
				});

				//清空数据
				DeviceEventEmitter.addListener("clearData", ()=>{
					// 清空所有数据
			        DeviceStorage.deleteArr(["@order@send","@order@buy","@order@express","@order@lockkey","@order@other"]);
				});




				// EasemobChat.startNativeActivity();
			//   Navigation.startTabBasedApp({
			// 	tabs: [
			// 		{
			// 			label: '消息',
			// 			screen: 'housekeeperapp.message',
			// 			icon: require('./img/hk_nav_msg.png'),
			// 			selectedIcon: require('./img/hk_nav_msg_active.png'),
			// 			title: '管家来了',
			// 			navigatorStyle,
			// 			navigatorButtons:{
			//                 rightButtons:[
			//                     {
			// 						title: '搜索',
			// 						icon: require('./img/search_active_icon.png'),
			//                         id: 'search',
			//                     }
			//                 ]
			//             }
			// 		},
			// 		{
			// 			label: '订单',
			// 			screen: 'housekeeperapp.order',
			// 			icon: require('./img/hk_nav_order.png'),
			// 			selectedIcon: require('./img/hk_nav_order_active.png'),
			// 			title: '我的订单',
			// 			navigatorStyle,
			// 			navigatorButtons:{
			//                 rightButtons:[
			//                     {
			// 						title: '新增任务',
			//                         id: 'add',
			//                     }
			//                 ]
			//             }
			// 		},
			// 		{
			// 			label: '账户',
			// 			screen: 'housekeeperapp.account',
			// 			icon: require('./img/hk_nav_account.png'),
			// 			selectedIcon: require('./img/hk_nav_account_active.png'),
			// 			title: '我的账户',
			// 			navigatorStyle:{
			// 				...navigatorStyle,
			// 				navBarHidden: true
			// 			}
			// 		}
			// 	],
			// 	appStyle: {
			// 	   tabBarBackgroundColor: '#ffffff',
			// 	   tabBarButtonColor: '#B5B5B5',
			// 	   tabBarSelectedButtonColor: '#EA5404',
			// 	   tabFontFamily: 'BioRhyme-Bold',
			// 	   forceTitlesDisplay: true
			// 	 },
			// 	animationType: 'fade'
			//   });
	        break;
	      default:
	        console.error('Unknown app root');
	    }
	}
}

export default App;
